package com.bw.module_splash

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import android.view.ViewParent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.common.adapter.ImageBannerAdapter
import com.bw.common.base.BaseActivity
import com.bw.common.entity.BannerEntity
import com.bw.common.path.RouthPath
import com.bw.module_splash.databinding.ActivitySplachBinding
import com.bw.router.LocalDegradeService
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SplachActivity : BaseActivity<ActivitySplachBinding>() {
    private lateinit var mImgAdapter:ImageBannerAdapter
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mCountDownTimer: CountDownTimer

    override fun initViews() {
        //判断是否是第一次启动
        var isFirstBoot = MMKV.defaultMMKV().decodeBool("isFirstBoot",true)
        if(isFirstBoot){
             MMKV.defaultMMKV().encode("isFirstBoot",false)
            //显示引导页
            mDataBind.clSplashApp.visibility = View.VISIBLE
            bannerData.add(BannerEntity(R.drawable.welcome_0,""))
            bannerData.add(BannerEntity(R.drawable.welcome_1,""))
            bannerData.add(BannerEntity(R.drawable.welcome_3,""))
            mImgAdapter = ImageBannerAdapter(bannerData)
            mDataBind.appBanner.setAdapter(mImgAdapter)

            //与当前Activity的生命周期关联
            mDataBind.appBanner.addBannerLifecycleObserver(this)
            //设置指示器的样式
            mDataBind.appBanner.indicator=CircleIndicator(this)
            //设置切换的过渡效果
            mDataBind.appBanner.addPageTransformer(AlphaPageTransformer())
            //设置切换事件监听
            mDataBind.appBanner.addOnPageChangeListener(object :OnPageChangeListener{
                override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

                }

                override fun onPageSelected(position: Int) {
                    if(position == bannerData.size - 1){
                        mDataBind.tvStart.visibility = View.VISIBLE

                    }else{
                        mDataBind.tvStart.visibility=View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(p0: Int) {

                }

            })
            //立即进入按钮点击事件
            mDataBind.tvStart.setOnClickListener {
                jumpToMainPage()
            }

        }
        //显示广告
        else{
            mDataBind.clSplashAdv.visibility = View.VISIBLE
            mCountDownTimer = object :CountDownTimer(3000,1000){
                override fun onTick(p0: Long) {
                    mDataBind.tvSplashSkip.text="${p0/1000}秒跳过"
                }

                override fun onFinish() {
                    this@SplachActivity.jumpToMainPage()
                }

            }.start()
        }
    }
    private fun jumpToMainPage(){
//        val token =MMKV.defaultMMKV().decodeString("token","")
        ARouter.getInstance().build("/new/activity").navigation(this,LocalDegradeService(RouthPath.PATH_LOGIN_PAGE,"login"))//使用局部降级策略，此时全局降级策略将不会执行
//        ARouter.getInstance().build("/new/activity").navigation(this)
        this@SplachActivity.finish()
    }

    override fun getLayoutId(): Int =R.layout.activity_splach

}